# coding=utf-8
from django.shortcuts import render
from app import models
from app.forms import FormB, FormC

def one_to_one(**kwargs):
    a = kwargs.get('a')
    A = models.A.objects.get_or_create(a=a)[0]
    print 'Create instance A:', A
    
    d={}
    d['a'] = 'aaa'
    d['b'] = A.id
    print 'init information for instance B:', d
    
    B = FormB(d)
    if B.is_valid():
        B = B.save()
    print 'Create instance B:', B
    

def many_to_many(**kwargs):
    a = kwargs.get('a')
    A = models.A.objects.get_or_create(a=a)[0]
    print 'Create instance A:', A
    print type(kwargs)

    c = kwargs.get('c', None)
    if c:
        kwargs['c'] = c.split(',')
        print kwargs['c']
        print type(kwargs['c'])
        
    print 'init information for instance C:', kwargs
    
    
    C = FormC(kwargs)
    if C.is_valid():
        C = C.save()
    else:
        print C.errors
    print 'Create instance C:', C

    